import random

from django.core.mail import send_mail
from django.template import loader

from AXF import settings

#发送邮件
from App.models import Cart, OrderGoods


def sendEmail(username,email,u_token):
    title = '激活'
    msg = ' '
    data = {
        'username': username,
        'url': 'http://127.0.0.1:8000/app/active/?u_token=' + u_token
    }
    temp = loader.get_template('user/active.html')
    html = temp.render(data)
    receiver = [
        email,
    ]

    send_mail(title,
              msg,
              from_email=settings.DEFAULT_FROM_EMAIL,
              recipient_list=receiver,
              html_message=html,

              )
#生成验证码

def get_color():
    return random.randrange(256)

def generate_code():
    source = "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM"
    code = ""
    for i in range(4):
        code += random.choice(source)
    return code

def get_total_price():
    carts = Cart.objects.filter(c_is_select=True)
    total = 0
    for cart in carts:
        total += float(cart.c_goods.price) * int(cart.c_goods_num)
    return "{:.2f}".format(total)
def get_final_price():
    final_price = 0
    ordergoods_list = OrderGoods.objects.all()
    for ordergoods in ordergoods_list:
        final_price += float(ordergoods.o_goods.price) * int(ordergoods.o_goods_num)
    return "{:.2f}".format(final_price)